package com.tust.androidreader;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {
    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();//隐藏标题导航栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去除时间和电量等状态栏
        setContentView(R.layout.activity_register);

        EditText editTextUserName = this.findViewById(R.id.et_username);
        EditText editTextPassword = this.findViewById(R.id.et_password);
        EditText editTextConfirmPassword = this.findViewById(R.id.et_confirmpassword);
        Button buttonRegister = this.findViewById(R.id.bt_register);
        Button buttonCancel = this.findViewById(R.id.bt_cancel);

        buttonRegister.setBackgroundColor(Color.rgb(153, 217, 234));
        buttonCancel.setBackgroundColor(Color.rgb(153, 217, 234));

        DBHelper dbHelper = new DBHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = editTextUserName.getText().toString();
                String password = editTextPassword.getText().toString();
                String confirmPassword = editTextConfirmPassword.getText().toString();

                if (userName.equals("")) {
                    showToast("用户名不能为空！");
                } else if (password.equals("")) {
                    showToast("密码不能为空！");
                } else if (confirmPassword.equals("")) {
                    showToast("确认密码不能为空！");
                } else  if (userName.equals("输入用户名")) {
                    showToast("请输入用户名！");
                } else if(password.equals("输入密码")) {
                    showToast( "请输入密码！");
                } else if(confirmPassword.equals("确认密码")) {
                    showToast("请确认密码！");
                } else {
                    if (password.equals(confirmPassword)) {
                        Cursor userCursor = db.rawQuery("select * from user where username = ?", new String[]{userName});
                        if (userCursor.getCount() < 1) {//没有找到该用户名
                            db.execSQL("insert into user (username, password) values (?, ?)", new Object[]{userName, password});
                            showToast("注册成功！");
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            startActivity(intent);
                        } else {
                            showToast("用户名已存在！");
                        }
                        userCursor.close();
                    } else {
                        showToast("两次密码不一致！");
                    }
                }
            }
        });

        buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    private void showToast(String s) {
        if (toast == null){
            toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
        }else {
            toast.cancel();
            toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
        }
        toast.show();
    }
}